package com.myjianzhu.zzt88.carouse;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.myjianzhu.zzt88.exercise.R;

import java.util.ArrayList;
import java.util.List;

public class CarouseView extends FrameLayout {

    private final String TAG = CarouseView.class.getSimpleName();
    private List<ImageView> iv_dots;
    private List<Drawable> subViews;

    private int FAKE_BANNER_SIZE = 6;
    private ViewPager vp;
    private LinearLayout ll_dot;
    private int count;


    public CarouseView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

    public CarouseView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CarouseView(Context context) {
        this(context, null);
    }


    void initLayout() {
        View view = LayoutInflater.from(getContext()).inflate(
                R.layout.view_carousel_layout, this, true);
        vp = (ViewPager) view.findViewById(R.id.vp);
        ll_dot = (LinearLayout) view.findViewById(R.id.ll_dot);
        ll_dot.removeAllViews();

        iv_dots = new ArrayList<>();
        subViews = new ArrayList<>();

        initSubViews();
        initDots();

        vp.setAdapter(new CarouseAdapter());
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int arg0) {
                for (int i = 0; i < iv_dots.size(); i++) {
                    int arg1 = arg0 % count;
                    if (i == arg1 ) {
                        iv_dots.get(i).setImageResource(R.mipmap.dot_focus);
                    } else {
                        iv_dots.get(i).setImageResource(R.mipmap.dot_blur);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

    }

    private void initDots() {
        for (int i = 0; i < count; i++) {
            ImageView iv_dot = new ImageView(getContext());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            params.leftMargin = 5;
            params.rightMargin = 5;
            iv_dot.setImageResource(R.mipmap.dot_blur);
            ll_dot.addView(iv_dot, params);
            iv_dots.add(iv_dot);
        }
        iv_dots.get(0).setImageResource(R.mipmap.dot_focus);
    }

    private void initSubViews() {

        subViews.add(getContext().getResources().getDrawable(R.drawable.white));
        subViews.add(getContext().getResources().getDrawable(R.drawable.blue));
        subViews.add(getContext().getResources().getDrawable(R.drawable.red));
        subViews.add(getContext().getResources().getDrawable(R.drawable.green));
        subViews.add(getContext().getResources().getDrawable(R.drawable.pink));

        count = subViews.size();
        FAKE_BANNER_SIZE = 2 * count;
    }


    private class CarouseAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return FAKE_BANNER_SIZE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Log.d(TAG, "instantiateItem position=" + position);
            position %= count;
            View view = LayoutInflater.from(getContext()).inflate(R.layout.item, container, false);
            TextView tv = (TextView) view.findViewById(R.id.item_tv);
            tv.setText("position" + position);
            tv.setBackground(subViews.get(position));

            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public void finishUpdate(ViewGroup container) {
            int position = vp.getCurrentItem();
            Log.d(TAG, "finish update before, position=" + position);
            if (position == 0) {
                position = count;
                vp.setCurrentItem(position, false);
            } else if (position == FAKE_BANNER_SIZE - 1) {
                position = count - 1;
                vp.setCurrentItem(position, false);
            }
            Log.d(TAG, "finish update after, position=" + position);
        }
    }


}
